package com.kaizzi.data.jdl.helper;

import com.kaizzi.data.common.been.TempBeen;
import com.kaizzi.data.common.utils.DateUtil;
import com.kaizzi.data.common.utils.JsonUtil;
import com.kaizzi.data.common.utils.LogicUtil;
import com.kaizzi.data.jdl.model.ExcelModel;
import com.kaizzi.data.jdl.service.UserService;
import lombok.Data;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;

/**
 * @author Kzen
 * @Description description
 * @date 2020/9/15
 */
@Component
public class PageData10 {

    @Resource
    private UserService userService;

    public String getData(ExcelModel model){
        Page10 page10 = new Page10();
        page10.setTitle(LogicUtil.getTitle(model,10));
        page10.setDataPeriod("2019.12.15-"+DateUtil.getDateFormat1(DateUtil.getLastSunday()));
        //原始数据传入 更好处理
        List<TempBeen> wesPullNew = userService.getWesPullNew(DateUtil.getLastSundayFormat2());

        for (TempBeen tempBeen : wesPullNew) {
            String realName = getRealName(tempBeen.getTempName());
            if (realName.equals("undefind")){
                continue;
            }
            tempBeen.setTempName(realName);
        }

        //日期集合
        Map<String, List<TempBeen>> collect = wesPullNew.stream().collect(Collectors.groupingBy(TempBeen::getTempData));

        for (String date : getDateList()) {
            if (collect.containsKey(date)){
                continue;
            }
            for (String wesShop : getWesShops()) {
                TempBeen tempBeen = new TempBeen();
                tempBeen.setTempCount(0);
                tempBeen.setTempName(wesShop);
                tempBeen.setTempData(date);
                wesPullNew.add(tempBeen);
            }
        }
        //名称集合
        Map<String,List<TempBeen>> result = wesPullNew.stream().collect(Collectors.groupingBy(TempBeen::getTempName));

        //先补全月份，再补全店铺
        for (String wesShop : getWesShops()) {
            List<TempBeen> beens = result.get(wesShop);
            if (getDateList().size() == beens.size()){
                continue;
            }
            List<String> dateList = getDateList();
            List<String> haveList = new ArrayList<>();
            for (TempBeen been : beens) {
                haveList.add(been.getTempData());
            }
            List<String> collect1 = dateList.stream().filter(item -> !haveList.contains(item)).collect(toList());
            for (String s : collect1) {
                TempBeen tempBeen = new TempBeen();
                tempBeen.setTempName(wesShop);
                tempBeen.setTempCount(0);
                tempBeen.setTempData(s);
                wesPullNew.add(tempBeen);
            }
        }
        //zhuanhuan
        List<TempBeen> beens = transBeen(wesPullNew);
        //新增总计
        Map<String, List<TempBeen>> collect1 = beens.stream().collect(Collectors.groupingBy(TempBeen::getTempName));
        for (String wesShop : getWesShops()) {
            List<TempBeen> beens1 = collect1.get(wesShop);
            double sum = beens1.stream().mapToDouble(TempBeen::getTempCount).sum();
            TempBeen tempBeen = new TempBeen();
            tempBeen.setTempName(wesShop);
            tempBeen.setTempData("总计");
            tempBeen.setTempCount((long) sum);
            beens.add(tempBeen);
        }

        Map<String, List<TempBeen>> collect2 = beens.stream().collect(Collectors.groupingBy(TempBeen::getTempData));
        for (String s : collect2.keySet()) {
            List<TempBeen> beens1 = collect2.get(s);
            long sum = beens1.stream().mapToLong(TempBeen::getTempCount).sum();
            TempBeen tempBeen = new TempBeen();
            tempBeen.setTempName("总计");
            tempBeen.setTempCount(sum);
            tempBeen.setTempData(s);
            beens.add(tempBeen);
        }

        page10.setWesList(beens);
        return JsonUtil.serialize(page10);
    }

    /**
     * @param name 数据库查询名称
     * @return 需转换名称
     */
    private String getRealName(String name){
        switch (name){
            case "威尔士中山公园店":
                return "中山公园店、大华朗香店";
            case "威尔士虹桥南丰城店":
                return "虹桥南丰城店、日月光店";
            case "威尔士古北店":
                return "古北店、长寿店";
            case "威尔士番禺店":
                return "番禺店、宝华紫薇";
            case "威尔士世纪公园店":
                return "世纪公园店、滨江苑";
            case "威尔士古北1699店":
                return "古北1699店、合生汇店";
            case "威尔士嘉园店":
                return "嘉园店、长江国际店";
            case "威尔士新天地店":
                return "新天地店、上滨广场";
            case "威尔士卢湾店":
                return "卢湾店、文峰广场店";
            case "威尔士汇景天地店":
                return "汇景天地店、百联世纪店";
            default:
                return "undefind";
        }
    }

    @Data
    private static class Page10{
        private String title;
        private String dataPeriod;
        private List<TempBeen> wesList;
    }

    private List<String> getWesShops(){
        return Arrays.asList("中山公园店、大华朗香店","虹桥南丰城店、日月光店","古北店、长寿店","番禺店、宝华紫薇","世纪公园店、滨江苑",
                "古北1699店、合生汇店","嘉园店、长江国际店","新天地店、上滨广场","卢湾店、文峰广场店","汇景天地店、百联世纪店");
    }

    //riqi
    private List<String> getDateList(){
        return DateUtil.getDateMonth();
    }

    //日期转为 2020年2月
    private List<TempBeen> transBeen(List<TempBeen> list){
        for (TempBeen tempBeen : list) {
            tempBeen.setTempData(transStr(tempBeen.getTempData()));
        }
        return list;
    }

    private String transStr(String string){
        return string.replace("/","年")+"月";
    }
}
